/*
 * @(#)FolderRolloverTag.java
 *
 * Copyright (c) 2003 DCIVision Ltd
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of DCIVision
 * Ltd ("Confidential Information").  You shall not disclose such Confidential
 * Information and shall use it only in accordance with the terms of the license
 * agreement you entered into with DCIVision Ltd.
 */

package com.dcivision.dms.taglib;

import java.text.NumberFormat;
import java.util.Locale;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.BodyTagSupport;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.impl.LogFactoryImpl;

import com.dcivision.framework.GlobalConstant;
import com.dcivision.framework.SessionContainer;
import com.dcivision.framework.SystemParameterConstant;
import com.dcivision.framework.SystemParameterFactory;
import com.dcivision.framework.TextUtility;

/**
  FolderRolloverTag.java

  <p>This class is to show the proportion of storage used and unused. </p>
  <p>Modification in "dms.tld"<br>
  <pre>
     [ADDED]
     &lt;tag&gt;
        &lt;name&gt;auditTrail&lt;/name&gt;
        &lt;tagclass&gt;com.dcivision.framework.taglib.layout.AuditTrailTag&lt;/tagclass&gt;
    &lt;/tag&gt;
  </pre>
  </p>

  <p>USAGE:</p>
  <p>The following example shows how to use folderRolloverTag tag.</p>
  <p><code>&lt;dms:folderRolloverTag/&gt;</code></p>

    @author          Weison Liang
    @company         DCIVision Limited
    @creation date   24/01/2005
    @version         $Revision: 1.3.2.1 $
*/

public class FolderRolloverTag extends BodyTagSupport {

  public static final String REVISION = "$Revision: 1.3.2.1 $";

  public FolderRolloverTag() {
    super();
  }
  
  private String sUsageOfFolder;
  
  private String sNotUseOfFolder;
  
  private String sStorageLength; 

  /**
   * doStartTag
   *
   * Show create date, creator, update date, updator and update count of a user record
   *
   * @param      No pass in parameter
   * @return     int
   * @throws     JspException
   */
  
  public void setsUsageOfFolder(String usageOfFolder){
    this.sUsageOfFolder = usageOfFolder;
  }
  
  public String getsUsageOfFolder(){
    return this.sUsageOfFolder;
  }
  
  public void setsNotUseOfFolder(String notUseOfFolder){
    this.sNotUseOfFolder = notUseOfFolder;
  }
  
  public String getsNotUseOfFolder(){
    return this.sNotUseOfFolder;
  }
  
  public void setsStorageLength(String storageLength){
    this.sStorageLength = storageLength;
  }
  
  public String getsStorageLength(){
    return this.sStorageLength;
  }
  
  public int doStartTag() throws JspException {
    Log log = new LogFactoryImpl().getInstance(this.getClass());
    HttpServletRequest request = (HttpServletRequest)pageContext.getRequest();
    Locale loc = (Locale)request.getSession().getAttribute(org.apache.struts.Globals.LOCALE_KEY);
    String preference = SystemParameterFactory.getSystemParameter(SystemParameterConstant.PREFERENCE);
    String contextPath = (String)this.pageContext.getServletContext().getAttribute(GlobalConstant.CONTEXT_PATH_KEY);
    SessionContainer sessionContainer = (SessionContainer)this.pageContext.getSession().getAttribute(GlobalConstant.SESSION_CONTAINER_KEY);
    float notUse=100-TextUtility.parseFloat(this.sUsageOfFolder)/TextUtility.parseFloat(this.sStorageLength)*100;
    if ((sessionContainer!=null) && (sessionContainer.getUserRecord()!=null)) {
        if (sessionContainer.getUserRecord().getPreference()!=null) {
          preference = sessionContainer.getUserRecord().getPreference().toString();
        }
      }
    int storage=TextUtility.parseInteger(this.sStorageLength);
    String storLength=TextUtility.formatFileSize(storage);
    NumberFormat nf = NumberFormat.getInstance();
    StringBuffer str = new StringBuffer();
    float precent = (TextUtility.parseFloat(this.sUsageOfFolder)/TextUtility.parseFloat(this.sStorageLength))*100;
    if (storage >= 1024) {
        str.append(nf.format(storage / 1024.0)).append(" GB");
    } else {
       str.append(nf.format(storage)).append(" MB");
    }
    
      // display the details
    try {
        JspWriter out = pageContext.getOut();
        out.println("<style>                    ");
        out.println("  .volume {background- color: white; border-top:solid #F5F5F5 1px; border-left:solid #F5F5F5 1px; border-right:solid #E9E9E9 1px; border-bottom:solid #E9E9E9 1px;}");
        out.println("</style>  ");
        out.println("<table> ");
        out.println(" <tr> ");
        out.println("  <td >  ");
        out.println(" Already used:"+ precent +"% Total:"+ str.toString() );
        out.println("  </td> ");
        out.println("  <td>");
        out.println("  <table  width='100' border='0' cellspacing='2' cellpadding='0'>");
        out.println("    <tr>");
        out.println("      <td> ");
        out.print("<img ");
        out.print("src=\"" +contextPath+ "/theme/pref" + preference + "/img/common/"+"StorageUse.gif"+ "\" ");
        out.print("align=\"absmiddle\" ");
        out.print("width=\""+ TextUtility.parseFloat(this.sUsageOfFolder)/TextUtility.parseFloat(this.sStorageLength)*100 +"\"");
        out.print("height=\"10\"");
        out.print("/>");
        out.print("<img ");
        out.print("src=\"" +contextPath+ "/theme/pref" + preference + "/img/common/"+"StorageNotUse.gif"+ "\" ");
        out.print("align=\"absmiddle\" ");
        out.print("width=\""+notUse+"\"");
        out.print("height=\"10\"");
        out.print("/>");
        out.println("     </td>");
        out.println("   </tr> ");
        out.println("  </table>");
        out.println(" </td>");
        out.println("</tr>");
        out.println("</table>");
      } catch (Exception e) {
        log.error("The FolderRolloverTag Error !", e);
        throw new JspException(e);
      }

    return SKIP_BODY;
  }
  
  public void release() {
    super.release();
  }
}